package com.ytjj.qmyx.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.mall.mapper.DynamicTopicDao;
import com.ytjj.qmyx.mall.mapper.DynamicTopicMapper;
import com.ytjj.qmyx.mall.model.request.DynamicTopicRequest;
import com.ytjj.qmyx.mall.model.response.DynamicResponse;
import com.ytjj.qmyx.mall.model.response.DynamicTopicResponse;
import com.ytjj.qmyx.mall.service.DynamicTopicService;
import com.ytjj.qmyx.users.client.AdminClient;
import com.ytjj.qmyx.users.client.BigdataClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 
 * 
 * @description:
 */
@Service
@Slf4j
public class DynamicTopicServiceImpl implements DynamicTopicService {

    @Resource
    private DynamicTopicMapper dynamicTopicMapper;

    @Resource
    private DynamicTopicDao dynamicTopicDao;

    @Autowired
    private AdminClient adminClient;
    @Autowired
    private BigdataClient bigdataClient;

    @Override
    public List<DynamicTopicResponse> getDynamicByIds() {
        //获取所有的话题
        List<DynamicTopicResponse> topicsByIds = dynamicTopicDao.getAllTopics();
        //从大数据库获取话题的Pv总量
//        CommonResult<List<Map<String,Object>>> someTopicPvTop = adminClient.getSomeTopicPvTop();
        CommonResult<List<Map<String,Object>>> someTopicPvTop = bigdataClient.topList();
        log.info("请求成功！");
        if(someTopicPvTop == null || someTopicPvTop.getCode() != 200){
            throw new ApiException("请求出错");
        }
        List<Map<String,Object>> maps =  someTopicPvTop.getData();
        topicsByIds.stream().forEach(e->{
            e.setTotalPv(e.getInitNum());
            //设置话题的访问量
            maps.stream().forEach(e2->{
                //如果Id相同，则赋值
                if(e.getId().equals(Integer.valueOf(e2.get("id") == null ? "0" : e2.get("id").toString()))){
                    //赋值访问量
                    e.setTotalPv(Integer.valueOf(e2.get("pvTotal") == null ? "0" : e2.get("pvTotal").toString())+
                            Integer.valueOf(e2.get("pvTotalFake") == null ? "0" : e2.get("pvTotalFake").toString())+
                            e.getInitNum());
                }
            });
        });
        //降序排列
//        topicsByIds = topicsByIds.stream().sorted(Comparator.comparing(DynamicTopicResponse::getTotalPv).reversed()).collect(Collectors.toList());
        return topicsByIds;
    }

    @Override
    public CommonPage getDynamicsByTopic(DynamicTopicRequest dynamicTopicRequest) {
        PageHelper.startPage(dynamicTopicRequest.getPageNum(),dynamicTopicRequest.getPageSize());
        List<DynamicResponse> dynamicResponses =  dynamicTopicDao.getDynamicsByTopic(dynamicTopicRequest.getId());
        return CommonPage.restPage(dynamicResponses);
    }

    @Override
    public Map<String,Object> getTopicInfoPvById(Integer id) {
        CommonResult<Map<String,Object>> topicInfoAndPvById = adminClient.getTopicInfoAndPvById(id);
        if(topicInfoAndPvById.getCode() != 200){
            throw new ApiException("请求出错");
        }
        Map<String,Object> map = topicInfoAndPvById.getData();
        return map;
    }
}
